from tools.utils import load_config
from tools.utils import get_chinese_name_to_ts_code
from tools.utils import concat_and_build_dir
from tools.utils import first_dir_exists
from tools.utils import check_str
from tools.utils import find_spot_or_stock_by_ts_code


if __name__ == "__main__":
    mode = "现货"
    cfg = load_config()
    path = first_dir_exists()
    cnttc = get_chinese_name_to_ts_code(cfg)
    for category, chinese_names in cfg['tjd_category'].items():
        category_path = concat_and_build_dir(path, category)
        for chinese_name in chinese_names:
            chinese_name_path = concat_and_build_dir(category_path, chinese_name)
            v = find_spot_or_stock_by_ts_code(cfg, cnttc[chinese_name], mode)
            for k, df in v.items():
                final_path = concat_and_build_dir(chinese_name_path, mode)
                save_path = f"{final_path}/{k}.xlsx"
                df.to_excel(check_str(save_path))
